var time = require('../../utils/util(1).js');
Page({
  data: {
    flag:1,
    formats: {},
    bottom: 0,
    readOnly: false,
    placeholder: '开始输入...',
    _focus: false,
    navTop: 0,
    topicId: -1,
    themeMessage: [],
    speech: false,
    src: "",
    uploadPictureUrl:"../../image/数学题/1.png"
  },
  //预览
  priview() {
    this.editorCtx.getContents({
      success(res) {

        wx.setStorageSync("priviewHtml", res.html);

        wx.navigateTo({
          url: '/pages/add_detail/add_detail',
        });

        // console.log('内容:', res.html)
      }
    })
  },

  uploadPicture(){
    var _this = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        _this.editorInsertImg(res.tempFilePaths[0])
      }
  });
  },

  editorInsertImg(filePath) {
    let self = this;
    // 富文本编辑器插入图片方法
    self.editorCtx.insertImage({
      src: filePath,
      width: "100%",
      data: {
        id: "imgage",
        role: "god",
      },
      // height: '50px',
      // extClass: className
    });
  },

  onLoad(options) {
    this.data.flag = options.flag
    this.data.topicId = options.topicId
    let that = this;
    this.setData({
      navTop: getApp().globalData.navTop,
      heightConfig: getApp().globalData.windowHeight,
      themeMessage: wx.getStorageSync("themeMessage")
    })

    wx.showToast({
      title: '已开启自动保存',
      icon:"none"
    })

    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
      that.editorCtx.setContents({
        "html": wx.getStorageSync("priviewHtml")
      })
    }).exec()



    // if (options.id != null) {
    //   this.setData({
    //     topicId: id
    //   })
    // }
  },
  formSubmit(e) {
    let that = this
    this.editorCtx.getContents({
      success(res) {

        if (res.html.length <= 13) {
          wx.showToast({
            title: '输入点东西吧',
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false,
          });
          return
        }

        wx.showModal({
          title: '提示',
          content: '确定发布么？',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {

            if (result.confirm) {
              wx.showLoading({
                title: "发布中",
              });
              if(that.data.flag==1){
                console.log(that.data.uploadPictureUrl);
                wx.request({
                  url: "http://localhost:8081/exam/addTopic",
                  data: {
                    title: res.text,
                    submitImg:that.data.uploadPictureUrl
                  },
                  success: (result) => {
                    wx.hideLoading();
                    wx.showModal({
                      title: '提示',
                      content: "添加成功",
                      confirmText: '确定',
                      showCancel: false,
                    });
                    wx.redirectTo({
                      url: '/pages/community/community',
                    })
                  },
                });
              }else if(that.data.flag==2){
                wx.request({
                  url: 'http://localhost:8081/exam/addComment',
                  data:{topicId:that.data.topicId,comment:res.text},
                  success: (result) => {
                    wx.hideLoading();
                    wx.showModal({
                      title: '提示',
                      content: "添加成功",
                      confirmText: '确定',
                      showCancel: false,
                    });
                    wx.redirectTo({
                      url: '/pages/topicDetail/topicDetail?id='+that.data.topicId,
                    })
                  },
                })
              }
              that.clear();
            }
          },
        })
      }
    })
  },

  bindPickerChange(e) {
    // 选择了课程类别
    console.log(e.detail.value)
    this.setData({
      index: e.detail.value
    })

  },
  readOnlyChange() {
    this.setData({
      readOnly: !this.data.readOnly
    })
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },

  undo() {
    this.editorCtx.undo()
  },
  redo() {
    this.editorCtx.redo()
  },
  format(e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({
      formats: formats
    })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function () {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("clear success")
      }
    })
  },
  removeFormat(e) {
    console.log(e)
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
  input(){
    this.editorCtx.getContents({
      success(res) {
        if (res.html.length != 0) {
          wx.setStorageSync("priviewHtml", res.html);
        }

      }
    })
  }
})